package org.four.merchant.service.impl;

import org.four.common.model.*;
import org.four.common.utils.Md5Util;
import org.four.merchant.mapper.MerchantMapper;
import org.four.merchant.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;

    //登陆：查看账号
    @Override
    public ComUserBean findUserInfoByAccount(String userName) {
        return merchantMapper.findUserInfoByAccount(userName);
    }

    //注册
    @Override
    public Map regedit(ComUserBean comUserBean, String pwd1) {
        //总返回
        HashMap<String, Object> result = new HashMap<>();
        //判断密码是否一致
        if (!comUserBean.getPassWord().equals(pwd1)){
            result.put("code", 2);
            result.put("msg", "密码不一致,请从新输入");
            return result;
        }
        //判断用户是否存在
        ComUserBean user = merchantMapper.findUserInfoByAccount(comUserBean.getUserName());
        if(user!=null) {
            result.put("code", 1);
            result.put("msg", "用户名已存在,请重新输入");
            return result;
        }
        String md532 = Md5Util.getMd532(comUserBean.getPassWord());
        comUserBean.setPassWord(md532);
        merchantMapper.regedit(comUserBean);
        result.put("code", 0);
        result.put("msg", "注册成功");
        return result;
    }

    //完善信息：修改回显数据
    @Override
    public ComUserBean addDatum(Integer id) {
        return merchantMapper.addDatum(id);
    }

    //账号信息
    @Override
    public ResultBean companyMessage(Integer id) {
        List<ComUserBean> list = merchantMapper.companyMessage(id);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("成功");
        resultBean.setData(list);
        return resultBean;
    }

    //完善信息:修改保存数据
    @Override
    public ComUserBean saveCompanyMessage(ComUserBean comUserBean) {
        return merchantMapper.savCompanyMessage(comUserBean);
    }

    //修改密码
    @Override
    public void editPassWord(Integer id,String md532) {
        merchantMapper.editPassWord(id,md532);
    }

    //公司：账户查询
    @Override
    public ResultBean findCompanyAccoumt(Integer id) {
        List<ComTel> comTels = merchantMapper.findCompanyAccoumt(id);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("成功");
        resultBean.setData(comTels);
        return resultBean;
    }

    //公司:绑定账号修改 回显
    @Override
    public ComTel findAccountById(Integer id) {
        return merchantMapper.findAccountById(id);
    }

    //公司:绑定账号新增、修改
    @Override
    public void saveCompanyAccount(ComTel comTel) {
        if(comTel.getId()==null){
            //新增
            merchantMapper.saveCompanyAccount(comTel);
        }else{
            //修改
            merchantMapper.updateCompayAccount(comTel);
        }

        ComUserBean comUserBean = new ComUserBean();
        comUserBean.setId(comTel.getComId());
        comUserBean.setTel(comTel.getTel());
        merchantMapper.updateAccount(comUserBean);
    }

    //查询公司信息
    @Override
    public ResultBean findConypanyMessage(Integer id) {
        List<CompanyMessage> companyMessages = merchantMapper.findConypanyMessage(id);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("成功");
        resultBean.setData(companyMessages);
        return resultBean;
    }

    //修改信息：回显
    @Override
    public CompanyMessage findCompanyMessageById(Integer id) {
        return merchantMapper.findCompanyMessageById(id);
    }

    //修改公司信息
    @Override
    public void saveCompanyAccountMessage(CompanyMessage companyMessage) {
        if(companyMessage.getId()!=null){
            merchantMapper.updateCompanyAccountMessage(companyMessage);
        }else{
            merchantMapper.saveCompanyAccountMessage(companyMessage);
        }
        
    }

    //查询套餐
    @Override
    public ResultBean findTaoCan(Integer limit,Integer page) {
        Integer count = merchantMapper.findCount();
        int start = (page-1)*limit;
        int end = limit;
        List<TaoCanBean> list =  merchantMapper.findTaoCan(start,end);
        ResultBean resultBean = new ResultBean(0,count,"成功",list);
        return resultBean;
    }


}

